package it.trekker.map;

import java.util.ArrayList;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> {
	
	protected static final String TAG = MyItemizedOverlay.class.getSimpleName();
	
	protected ArrayList<OverlayItem> overlayItemList = new ArrayList<OverlayItem>();
	
	
	public MyItemizedOverlay(Drawable marker) {
		super(boundCenterBottom(marker));

		populate();
	}
	
	public void addItem(GeoPoint p, String title, String snippet) {
		OverlayItem newItem = new OverlayItem(p, title, snippet);
		overlayItemList.add(newItem);
		populate();
	}
	
	public void setItem(GeoPoint p, String title, String snippet) {
		overlayItemList.removeAll(overlayItemList);
		OverlayItem newItem = new OverlayItem(p, title, snippet);
		overlayItemList.add(newItem);
		populate();
	}
	
	public void removeAllItems() {
		overlayItemList.removeAll(overlayItemList);
	}
	
	public void removeLastItem() {
		overlayItemList.remove(overlayItemList.size()-1);
	}
	
	protected OverlayItem nextItem(OverlayItem current) {
		int i = overlayItemList.indexOf(current);
		if(i < overlayItemList.size()-1) {
			i++;
		}
		else {
			i = 0;
		}
		return overlayItemList.get(i);
	}
	
	protected OverlayItem prevItem(OverlayItem current) {
		int i = overlayItemList.indexOf(current);
		if(i > 0) {
			i--;
		}
		else {
			i = overlayItemList.size()-1;
		}
		return overlayItemList.get(i);
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return overlayItemList.get(i);
	}
	
	@Override
	public int size() {
		return overlayItemList.size();
	}
	
	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		super.draw(canvas, mapView, shadow);
		//boundCenterBottom(marker);
	}
}
